<?php
function log_error($num, $str, $file, $line)
{
    $message = date("Y-m-d H:i:s") . " 文件：{$file},行数：{$line},错误信息：{$str}";
    file_put_contents("../api_resource/logs/" . date("Ymd") . "_sync.log", $message . PHP_EOL, FILE_APPEND);
}
set_error_handler("log_error");
//error_reporting(E_ALL);
include_once '../api_resource/mysql_pdo.php';

include_once '../api_resource/functions.php';


$params = $_GET;
$act = $params['act'] ?? '';

$product_ids = $params['product_id'] ?? '';

//$skip_hook = $params['skip_hook'] ?? 0;
$product_id_arr = explode(",", $product_ids);

if (count($product_id_arr) == 0) {
    error("product_id 参数有误");
}
$db = new mysql_pdo();

/**推送订单队列**/
if ($act == 'sync_channel_status') {
    set_time_limit(0);
    //获取最近3天的时间
    $starttime = strtotime('-3 day');
    $starttime = date('Y-m-d 00:00:00', $starttime);
    $starttime = strtotime($starttime);

    $endtime = strtotime('-1 hour');
    if (isset($params['time']) && $params['time'] == 1) {
        $endtime = strtotime('-10 minute');
    }

    $order_lists = select_table_muti(
        $db,
        'cz_order',
        [
            'created_at' => ['BETWEEN', "{$starttime} AND {$endtime}"],
            'product_id' => ['IN', "({$product_ids})"],
            'order_status' => 2,
            'is_auto' => 1
        ],
        [],
        [],
        1000
    );
    foreach ($order_lists as $order) {
        //查渠道订单 SELECT * FROM `cz_channel_order` WHERE order_sn='CZ20220701230054841990460' and channel_order_status=4; msg 充值中[38189606]
        //        $msg = $order['充值中[38189606]'];

        $channel_order = select_table_one($db, 'cz_channel_order', [
            'order_sn' => trim($order['order_sn']),
            'channel_order_status' => ['IN', '(1,2)']
        ]);
        //        echo "<p>{$order['product_name']}{$order['face_value']}----{$order['order_sn']}----{$order['mobile']}----{$order['id']}";

        if ($channel_order) {
            echo "<p>{$order['product_name']}{$order['face_value']}----{$order['order_sn']}----{$order['mobile']}----{$channel_order['channel_order_status']}----存在";
            continue;
        }

        $channel_order_last = select_table_one($db, 'cz_channel_order', [
            'order_sn' => trim($order['order_sn']),
        ], [], [
            ['id', 'desc']
        ]);
        //同步成功
        if ($channel_order_last['channel_order_status'] == 3) {
            $msg = '充值完成[系统状态同步]';
            $order_status = 3;
            update_table($db, 'cz_order', [
                'order_status' => 3,
                'msg' => $msg,
                'updated_at' => time(),
            ], [
                'order_sn' => $order['order_sn'],
                'order_status' => 2,
            ]);

            echo "<p>{$order['product_name']}{$order['face_value']}----{$order['mobile']}----{$msg}</p>";
        }
        //同步失败
        if ($channel_order_last['channel_order_status'] == 4) {
            $order_status = 4;
            $msg = '充值失败[系统状态同步]';

            update_table($db, 'cz_order', [
                'order_status' => 4,
                'msg' => $msg,
                'updated_at' => time(),
            ], [
                'order_sn' => $order['order_sn'],
                'order_status' => 2,
            ]);

            //异步退款
            sock_queue($order['id'], 'order_refund');

            echo "<p>{$order['product_name']}{$order['face_value']}----{$order['mobile']}----{$msg}</p>";
        }

        if ($order['callback_url']) {
            sock_queue($order['id'], 'callback_user_order');
        }
    }
    success([], "订单数量：" . count($order_lists));
}
error('未知请求操作');
